from django.db import models
from django.utils import timezone
from datetime import timedelta


# Create your models here.

class Question(models.Model):

    def __str__(self):
        return self.question_text

    # id = models.IntegerField(primary_key=True)
    id = models.AutoField(primary_key=True)
    question_text = models.CharField(max_length=200, null=False)
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        now = timezone.now()
        return now - timedelta(days=1) <= self.pub_date <= now

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'


class Choice(models.Model):

    def __str__(self):
        return self.choice_text

    # on_delete=models.CASCADE 级联删除，当主表中被删除是，从表的数据一起被删除
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
